import request from '../utils/request'

// 商家注册
export const merchantRegisterService = (merchantData: any, userId: number) => {
  return request.post('/merchant/register', merchantData, {
    params: { userId }
  })
}

// 商家登录
export const merchantLoginService = (name: string, password: string, userId: number) => {
  const params = new URLSearchParams()
  params.append('name', name)
  params.append('password', password)
  params.append('userId', String(userId))
  return request.post('/merchant/login', params)
}

// 获取商家信息
export const getMerchantInfoService = () => {
  return request.get('/merchant/merchantInfo')
}

// 更新商家基本信息
export const updateMerchantService = (merchantData: any) => {
  return request.put('/merchant/update', merchantData)
}

// 更新商家Logo
export const updateMerchantLogoService = (avatarUrl: string) => {
  return request.patch('/merchant/updateLogo', null, {
    params: { avatarUrl }
  })
}

// 更新商家密码
export const updateMerchantPwdService = (newPwd: string, rePwd: string) => {
  const params = new URLSearchParams()
  params.append('newPwd', newPwd)
  params.append('rePwd', rePwd)
  return request.patch('/merchant/updatePwd', params)
}

// 分页查询商家的商品
export const findItemsByMerchantIdService = (pageNum: number, pageSize: number) => {
  return request.get('/merchant/findItemsByMerchantId', {
    params: { pageNum, pageSize }
  })
}

// 模糊搜索商家的商品
export const searchItemsByMerchantIdService = (keyword: string, pageNum: number, pageSize: number) => {
  return request.get('/merchant/searchItemsByMerchantId', {
    params: { keyword, pageNum, pageSize }
  })
}

// 添加商品
export const addItemService = (itemData: any) => {
  return request.post('/merchant/addItem', itemData)
}

// 下架商品
export const disableItemService = (itemId: number) => {
  return request.put('/merchant/disableItem', null, {
    params: { itemId }
  })
}

// 批量下架商品
export const disableItemsService = (itemIds: number[]) => {
  const params = new URLSearchParams()
  itemIds.forEach(id => params.append('itemIds', String(id)))
  return request.put('/merchant/disableItems', null, {
    params
  })
}

// 上架商品
export const enableItemService = (itemId: number) => {
  return request.put('/merchant/enableItem', null, {
    params: { itemId }
  })
}

// 批量上架商品
export const enableItemsService = (itemIds: number[]) => {
  return request.put('/merchant/enableItems', itemIds)
}

// 更新商品信息
export const updateItemService = (item: any) => {
  return request.put('/merchant/updateItem', item)
}

// 删除商品
export const deleteItemService = (itemId: number) => {
  return request.delete('/merchant/deleteItem', {
    params: { itemId }
  })
}

// 批量删除商品
export const deleteItemsService = (itemIds: number[]) => {
  return request.delete('/merchant/deleteItems', {
    data: itemIds
  })
}

// 分页查询订单
export const findOrdersService = (
  status?: number | string | null,
  order_id?: string,
  name?: string,
  pageNum: number = 1,
  pageSize: number = 10
) => {
  return request.get('/merchant/findOrders', {
    params: {
      status: status || undefined,
      order_id,
      name,
      pageNum,
      pageSize
    }
  })
}

// 发货
export const deliverOrderService = (Id: number) => {
  return request.put('/merchant/deliver', null, {
    params: { Id }
  })
}

// 退款
export const refundOrderService = (Id: number) => {
  return request.put('/merchant/refund', null, {
    params: { Id }
  })
}
